package com.j.lemon.learn.thread.CAS;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author lijunjun
 * @Date 2019-04-17 15:57
 * @Description 原子引用类
 */
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User zh3 = new User("z3",18);
        User li4 = new User("li4",20);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(zh3);

        System.out.println(atomicReference.compareAndSet(zh3, li4));
        System.out.println(atomicReference.compareAndSet(zh3, li4));
        System.out.println(atomicReference.get());

    }
}


class User{
    private String name;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
